package com.example.demo.common;


import com.example.demo.interceptor.WebLogHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by lyc on 2019/5/31.
 * 返回结果工厂
 */
public class ResponseResultFactory {

    private static final Logger LOGGER = LoggerFactory.getLogger(ResponseResultFactory.class);

    public static ResponseResult success(String message) {
        return new ResponseResult(ResponseResultCode.SUCCESS.getCode(), message);
    }

    public static ResponseResult success(String message, Object data) {
        return new ResponseResult(ResponseResultCode.SUCCESS.getCode(), message, data);
    }

    public static ResponseResult exceptionFail(String message) {
        ResponseResult responseResult = new ResponseResult(ResponseResultCode.FAIL_EXCEPTION.getCode(), message);
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        LOGGER.info("错误响应response:{},traceId:{}",responseResult,request.getAttribute(WebLogHandler.TRACEID));
        return responseResult;
    }

    public static ResponseResult exceptionFail(String message, Object data) {
        ResponseResult responseResult  = new ResponseResult(ResponseResultCode.FAIL_EXCEPTION.getCode(), message, data);
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        LOGGER.info("错误响应response:{},traceId:{}",responseResult,request.getAttribute(WebLogHandler.TRACEID));
        return responseResult;
    }

    public static ResponseResult authorityFail(String message) {
        ResponseResult responseResult = new ResponseResult(ResponseResultCode.FAIL_AUTHORITY.getCode(), message);
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        LOGGER.info("错误响应response:{},traceId:{}",responseResult,request.getAttribute(WebLogHandler.TRACEID));
        return responseResult;
    }

    public static ResponseResult authorityFail(String message, Object data) {
        ResponseResult responseResult = new ResponseResult(ResponseResultCode.FAIL_AUTHORITY.getCode(), message, data);
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        LOGGER.info("错误响应response:{},traceId:{}",responseResult,request.getAttribute(WebLogHandler.TRACEID));
        return responseResult;
    }

    public static ResponseResult fieldFail(String message) {
        ResponseResult responseResult = new ResponseResult(ResponseResultCode.FAIL_FIELD.getCode(), message);
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        LOGGER.info("错误响应response:{},traceId：{}",responseResult,request.getAttribute(WebLogHandler.TRACEID));
        return responseResult;
    }

    public static ResponseResult fieldFail(String message, Object data) {
        ResponseResult responseResult = new ResponseResult(ResponseResultCode.FAIL_FIELD.getCode(), message, data);
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        LOGGER.info("错误响应response:{},traceId:{}",responseResult,request.getAttribute(WebLogHandler.TRACEID));
        return responseResult;
    }

    public static ResponseResult other(int code, String message) {
        ResponseResult responseResult = new ResponseResult(code, message);
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        LOGGER.info("错误响应response:{},traceId:{}",responseResult,request.getAttribute(WebLogHandler.TRACEID));
        return responseResult;
    }

    public static ResponseResult other(int code, String message, Object data) {
        ResponseResult responseResult = new ResponseResult(code, message, data);
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        LOGGER.info("错误响应response:{},traceId:{}",responseResult,request.getAttribute(WebLogHandler.TRACEID));
        return responseResult;
    }
}
